<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="900px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="128px"
      :model="formData"
      :rules="rules"
      size="medium"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="formData.name" clearable placeholder="名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="失效天数" prop="valid_days">
            <el-input
              v-model="formData.valid_days"
              clearable
              placeholder="失效天数"
              type="number"
            >
              <template slot="append">天</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="贮存条件" prop="temperature_self">
            <el-input
              v-model="formData.temperature_self"
              clearable
              placeholder="贮存条件"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制备失效时间" prop="idx">
            <el-input
              v-model="formData.pre_hours"
              clearable
              placeholder="排序"
              type="number"
            >
              <template slot="append">小时</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="入库时默认血量" prop="capacity_init">
            <el-input
              v-model="formData.capacity_init"
              clearable
              placeholder="入库时默认血量"
              type="number"
            >
              <template slot="append">U</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="U与ml换算系数" prop="kunit">
            <el-input
              v-model="formData.kunit"
              clearable
              placeholder="血量U与ml换算系数"
              type="number"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="血液成分码" prop="product_code">
            <el-input
              v-model.trim="formData.product_code"
              clearable
              placeholder="血液成分码"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="idx">
            <el-input
              v-model="formData.idx"
              clearable
              placeholder="排序"
              type="number"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submit('mainForm')">提交</el-button>
        <el-button @click="reset('mainForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
  import { updateOne as doEdit, addOne as doAdd } from '@/api/product'
  import { bloodGroupsFilter } from '@/utils/constant'
  import { removeEmptyField } from '@/utils'
  export default {
    data() {
      return {
        formData: {
          id: '',
          name: '',
          temperature_self: '',
          idx: '',
        },
        rules: {
          name: [{ required: true, trigger: 'blur', message: '名称必须' }],
          temperature_self: [
            { required: true, trigger: 'blur', message: '贮存条件必须' },
          ],
        },
        activeName: 'first',
        bloodGroupsFilter,
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.formData = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.formData = {
          id: '',
          name: '',
          temperature_self: '',
          idx: '',
        }
        this.dialogFormVisible = false
      },
      submit() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title === '编辑') {
              let { msg } = await doEdit(
                this.formData.id,
                removeEmptyField(this.formData)
              )
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              let { msg } = await doAdd(removeEmptyField(this.formData))
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
